//
//  HCWAddImageView.m
//  LRShop
//
//  Created by HCW on 16/8/1.
//  Copyright © 2016年 HCW. All rights reserved.
//

#import "HCWAddImageView.h"
#import "HCWAddImage.h"

#pragma mark - HCWAddImageModel

@implementation HCWAddImageModel

@end

#pragma mark - HCWAddImageCell

@implementation HCWAddImageCell

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self build];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self build];
    }
    return self;
}

- (void)build
{
    _iconImageView = [UIImageView new];
    _iconImageView.clipsToBounds = YES;
    _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_iconImageView];
    WEAK_SELF
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

@end

#pragma mark - HCWAddImageView

@interface HCWAddImageView() <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) HCWAddImage *addImage;
@property (nonatomic, strong) NSMutableArray<HCWAddImageModel *> *imageModels;
@end

@implementation HCWAddImageView
@synthesize imageModels = _imageModels;

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self build];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self build];
    }
    return self;
}

- (void)build
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake(60, 60);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collectionView];
    
    [_collectionView registerClass:[HCWAddImageCell class] forCellWithReuseIdentifier:@"HCWAddImageCell"];
    
    WEAK_SELF
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    if (self.maxNumber == 0) {
        self.maxNumber = 5;
    }
    
    HCWAddImageModel *model = [HCWAddImageModel new];
    model.image = [UIImage imageNamed:@"addpic2"];
    model.imageID = 1000;
    _imageModels = @[model].mutableCopy;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HCWAddImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HCWAddImageCell" forIndexPath:indexPath];
    
    HCWAddImageModel *model = self.imageModels[indexPath.row];
    if (model.image) {
        cell.iconImageView.image = model.image;
    } else {
        [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.imageURL] placeholderImage:[UIImage imageNamed:@"tqbm_pic_zp"]];
    }
    
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WEAK_SELF
    HCWAddImageModel *model = self.imageModels[indexPath.row];
    // 添加
    if (model.imageID == 1000) {
        if (self.imageModels.count > self.maxNumber) {
            [HUD showErrorMsg:[NSString stringWithFormat:@"最多只能添加%ld张照片", self.maxNumber]];
            return;
        }
        [self.addImage addImageFinishBlock:^(UIImage *image) {
            
            HCWAddImageModel *m = [HCWAddImageModel new];
            m.image = image;
            
            [weakSelf.imageModels insertObject:m atIndex:weakSelf.imageModels.count-1];
            [weakSelf.collectionView reloadData];
            CGFloat width = weakSelf.collectionView.contentSize.width;
            CGFloat selfWidth = weakSelf.collectionView.bounds.size.width;
            [weakSelf.collectionView setContentOffset:CGPointMake(width>selfWidth?width-selfWidth+70:0, 0) animated:YES];
        }];
    }
    // 替换
    else {
        [self.addImage addImageFinishBlock:^(UIImage *image) {
            
            HCWAddImageModel *m = [HCWAddImageModel new];
            m.image = image;
            
            [weakSelf.imageModels replaceObjectAtIndex:indexPath.row withObject:m];
            [weakSelf.collectionView reloadData];
            CGFloat width = weakSelf.collectionView.contentSize.width;
            CGFloat selfWidth = weakSelf.collectionView.bounds.size.width;
            [weakSelf.collectionView setContentOffset:CGPointMake(width>selfWidth?width-selfWidth+70:0, 0) animated:YES];
        }];
    }
    
}

- (NSArray<UIImage *> *)images
{
    NSMutableArray *images = [NSMutableArray new];
    for (HCWAddImageModel *model in self.imageModels) {
        if (model.imageID != 1000 && model.image) {
            [images addObject:model.image];
        }
    }
    
    return images;
}

- (NSArray<NSString *> *)photoURLs
{
    NSMutableArray *imageURLs = [NSMutableArray new];
    for (HCWAddImageModel *model in self.imageModels) {
        if (model.imageID != 1000 && model.imageURL) {
            [imageURLs addObject:model.imageURL];
        }
    }
    
    return imageURLs;
}

- (NSArray<HCWAddImageModel *> *)addImageModels
{
    NSMutableArray *imageModels = [NSMutableArray new];
    for (HCWAddImageModel *model in self.imageModels) {
        if (model.imageID != 1000) {
            [imageModels addObject:model];
        }
    }
    
    return imageModels;
}

#pragma mark - Getter Setter

- (HCWAddImage *)addImage
{
    if (!_addImage) {
        _addImage = [HCWAddImage new];
    }
    return _addImage;
}

- (void)setImageURLs:(NSArray<NSString *> *)imageURLs
{
    _imageURLs = imageURLs;
    
    NSMutableArray *ms = [NSMutableArray arrayWithCapacity:_imageURLs.count];
    for (int i=0; i<_imageURLs.count; i++) {
        HCWAddImageModel *m = [HCWAddImageModel new];
        m.imageURL = _imageURLs[i];
        [ms addObject:m];
    }
    self.imageModels = ms;
    
    [self.collectionView reloadData];
}

@end
